<?php

namespace App\Http\Controllers\Home;
use App\Models\Address;
use App\Models\Member;


/**
 * 订单控制器
 */
class AddressController extends BaseController
{

    public function __construct(Member $member,Address $address)
    {
        parent::__construct();
        $this->member = $member;
        $this->address = $address;

    }


    public function getIndex()
    {
        $where['mid'] = $this->mid;
        $where['shop_id'] = $this->shop_id;
        $where['is_default'] = 1;
        $ajaxData['info'] = $this->address->_getOne($where);
        if ($ajaxData['info']) {
            $ajaxData['info'] = $ajaxData['info']->toArray();
        }
        return success($ajaxData);
    }

    public function getList()
    {
        $where['mid'] = $this->mid;
        $ajaxData = [];
        if ($where['mid']) {
            $ajaxData['address_list'] = $this->address->_getList($where)->toArray();
        }
        return success($ajaxData);
    }

    /**
     * 确定收货地址
     */
    public function postAdd()
    {
        $res = $this->address->addOrUpdate();
        if (!$res) {
            return error($this->address->getError());
        }
        return success($res);
    }

    public function getEdit()
    {
        $ajaxData['info'] = $this->address->_getOneById(request('id'));
        if ($ajaxData['info']) {
            $ajaxData['info']['area_info'] = explode(' ',$ajaxData['info']['area_info']);
        }
        return success($ajaxData);
    }

    public function postEdit()
    {
        $res = $this->address->addOrUpdate();
        if (!$res) {
            return error($this->address->getError());
        }
        return success($res);
    }

    public function postSetDefault()
    {
        $res = $this->address->setDefault();
        if (!$res) {
            return error($this->address->getError());
        }
        return success('操作成功');
    }

    public function postDelete()
    {
        $res = $this->address->del();
        if (!$res) {
            return error($this->address->getError());
        }
        return success($res);
    }

}
